package ca.cbc.medialib.util;

import com.google.common.base.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Arash Bizhanzadeh <arash.bizhan-zadeh@cbc.ca>
 *         Date: 09/03/11
 *         Time: 11:49 AM
 *         Copyrighted to CBC. All rights reserved.
 */
public class DateConverter implements Function<String, Date> {
    private DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private static Logger logger = LoggerFactory.getLogger(DateConverter.class);

    @Override
    public Date apply(String s) {
        if (s == null) {
            return null;
        } else {
            try {
                return format.parse(s);
            } catch (ParseException e) {
                logger.warn("Bad date format in thePlatform {}", s);
                return null;
            }
        }
    }
}